﻿//decode *.lrc
//by CHikari
//v0.1
function lrc2Title(){
    var actItem = app.project.activeItem;    												//The activeItem in after effects
    if(actItem == null || !(actItem instanceof CompItem)) {    							//If the Item doesn't exists or isn't a comp
        alert("没有活动中的合成组");						//show a message and return
        return;
    }

    var laycoll = actItem.layers;   //get layerCollection laycoll
    
    
    var lrc= File.openDialog("选择LRC文件", "*.lrc");  
    lrc.open("r", "TEXT", "????");
    
    var reg = new RegExp("\\[(\\d{2}:\\d{2}\\.\\d{2})\\]");
    
    while(!(lrc.eof)){
        var line = lrc.readln();
        var time = reg.exec(line);
        if(time != null){
            laycoll.addText(line.replace(time[0], ""));
            var curlayer = actItem.layer(1);
            curlayer.startTime = currentFormatToTime(time[1], 60);
            try{
                var preLine = actItem.layer(2);
                preLine.outPoint = curlayer.startTime;
            }
            catch(err){
                
            }
            //laycoll.addText(currentFormatToTime(time[1], 60)+" || " + time[1]);   //using for debug
            curlayer.sourceText.setValueAtTime(currentFormatToTime(time[1], 60), line.replace(time[0], ""));
        }
    }
}

lrc2Title ();